using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_Client.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using DIS_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_Client.DIS_BUS_WS;
	#endregion
	public partial class FrmManageChemistWareHouse : Office2007Form
	{
		public EventHandler Handler { get; set; }

		public FrmManageChemistWareHouse(bool enableChon = false)
		{
			InitializeComponent();
			btnSelect.Enabled = enableChon;
		}

		private void FrmManageChemistWareHouse_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnSelect_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.CHOOSING, ref errorText))
			{
				MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			var id = int.Parse(txtID.Text);
			var name = BusService.Instance.ChemistWareHouse_SelectByID(id).Name;
			TransferDataInfo(this, new TransferSimpleDataEvent(id + "#" + name));
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.INSERT, ref errorText))
			{
                MessageBoxEx.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionAdd())
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Insert", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
				LoadData();
				btnReset_Click(sender, e);
			}
			else
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Update", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.DELETE, ref errorText))
			{
				MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
            if (MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Confirm"), "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				if (ActionDelete())
				{
                    MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
					LoadData();
					btnReset_Click(sender, e);
				}
				else
				{
                    MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.UPDATE, ref errorText))
			{
				MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionUpdate())
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Update", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
				LoadData();
			}
			else
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Update", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			txtID.Text = "";
			txtName.Text = "";
			txtPhone.Text = "";
			txtAddress.Text = "";
		}

		private void dtgvData_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (dtgvData.SelectedRows == null || dtgvData.SelectedRows.Count == 0)
				return;
			var selectedItem = dtgvData.SelectedRows[0];
			txtID.Text = dtgvData.SelectedRows[0].Cells["ID"].Value.ToString();
			var item = BusService.Instance.ChemistWareHouse_SelectByID(int.Parse(txtID.Text));
			LoadDetailInfo(item);
		}

		public void LoadDetailInfo(ChemistWareHouseDTO item)
		{
			txtName.Text = item.Name == null ? "" : item.Name.ToString();
			txtPhone.Text = item.Phone == null ? "" : item.Phone.ToString();
			txtAddress.Text = item.Address == null ? "" : item.Address.ToString();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Name",
				DataPropertyName = "Name",
				Name = "Name",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Phone",
				DataPropertyName = "Phone",
				Name = "Phone",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Address",
				DataPropertyName = "Address",
				Name = "Address",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}


		private void LoadData()
		{
			var lstItem = BusService.Instance.ChemistWareHouse_SelectAll();
			var dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("Name");
			dt.Columns.Add("Phone");
			dt.Columns.Add("Address");
			for (int i = 0; i < lstItem.Length; i++)
			{
				dt.Rows.Add(lstItem[i].ID, lstItem[i].Name, lstItem[i].Phone, lstItem[i].Address);
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private bool ValidateInput(EnumUpdateMode mode, ref string errorText)
		{
			if (mode == EnumUpdateMode.UPDATE || mode == EnumUpdateMode.DELETE)
			{
				if (txtID.Text == "")
				{
					errorText = "Please select ChemistWareHouse";
					return false;
				}
			}
			if (mode != EnumUpdateMode.DELETE)
			{
				if (txtName.Text == "")
				{
					 errorText = "Please enter Name";
					return false;
				}
				if (txtPhone.Text == "")
				{
					 errorText = "Please enter Phone";
					return false;
				}
				if (txtAddress.Text == "")
				{
					 errorText = "Please enter Address";
					return false;
				}
			}
			return true;
		}

		private bool ActionAdd()
		{
			try
			{
				var item = new ChemistWareHouseDTO
				{
					Name = txtName.Text,
					Phone = txtPhone.Text,
					Address = txtAddress.Text,
				};
				if (!BusService.Instance.ChemistWareHouse_Insert(item))
				{
					return false;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		private bool ActionUpdate()
		{
			try
			{
				var item = BusService.Instance.ChemistWareHouse_SelectByID(int.Parse(txtID.Text));
				item.Name = txtName.Text;
				item.Phone = txtPhone.Text;
				item.Address = txtAddress.Text;
				return BusService.Instance.ChemistWareHouse_Update(item);
			}
			catch
			{
				return false;
			}
		}

		private bool ActionDelete()
		{
			try
			{
				return BusService.Instance.ChemistWareHouse_Delete(int.Parse(txtID.Text));
			}
			catch
			{
				return false;
			}
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}



	}
}